



class MemberBYCompanyList extends BaseTableList  {

    constructor(props) {
        super(props);
        this.fixedKey="id";
        this.filter = [ 'level'];
        this.fetchUrl="/statistics/memberListByCompany";
        this.actionWidth=300;
        this.placeholder="请输入名称";
        this.actioncolumns = true;
        this.canRowSelection=false;
        this.vip_group_list=[{id:-1,name:'全部'}];
        this.columns=this.getColumns();
    }

    state = {
        data: [],
        vip_group_id:-1,
        pagination: {   //搜索参数
            current: 1,
            pageSize: 200,
            keywords: '',
            order: '',
            vip_group:-1,
            filter: {  level: ''},
            total: 0,
            showSizeChanger: true,
            pageSizeOptions: ['200','500'],
            showTotal: ((total) => {
                return `共 ${total} 条`;
            }),
            onShowSizeChange: (current, pageSize) => {
                const { pagination } = this.state;
                pagination.pageSize = pageSize;
                this.setState({
                    pagination
                });
            }
        },
        loading: false,
        visible: false,
        modalText: '确定删除？',
        selectedRowKeys: [],
        selectedRows: [],
    }

    res_data = {
        name: {
            label: '姓名',
            width: 100
        },
        score:{
            label: '等级分',
            width: 100,
        },
        score_type:{
            label: '等级',
            width: 100,
        },
        group_str:{
            label: '所在组',
            width: 150,
        }
    }

    CON_GROUP_SEL={
        "2022展会抽奖1_威程":56,
        "2022展会抽奖2_代龙":57,
        "2022展会抽奖3_vip":58,
        "2022展会抽奖4_普通":59,
    }


    async componentDidMount() {
        let vip_group_list=await window.M.gloableCache.vip_group_list.getValue();
        let con_groupList= Object.values(this.CON_GROUP_SEL);
        vip_group_list=  vip_group_list.filter(u=>con_groupList.includes(u.id))
        this.vip_group_list=[...this.vip_group_list,...vip_group_list];
        this.fetch();
    };


    filterContent(){
        const { pagination} = this.state;
        if(JSON.stringify(pagination.filter)=='{}') return <div></div>;
        const level = ['白银会员', '黄金会员', '铂金会员', '钻石会员'];
        return <div>
            <div style={{padding: '5px 0px 5px 0px'}}>
                <span style={{fontWeight: 'bolder'}}>{"会员等级："}</span>
                <CheckboxGroup options={level} value={pagination.filter.level.split(',')} onChange={(v) => this.filterType('level',v)} />
            </div>
        </div>
    }


    //点击排序
    vipGroupChange(v){
        this.setState({
            vip_group_id: v
        });
    }


    inputRender(){
        const {pagination, selectedRows, selectedRowKeys } = this.state;
        return <div>
            <Form style={{"display":"flex",padding: "24px 0 0 24px"}}>
                <div style={{flex: 1,display:  'flex'}}>
                    <Popover placement={'bottomLeft'} content={this.filterContent()} trigger="hover">
                        <Button style={{"marginRight": 15,"top": 4}}>{"筛选"}</Button>
                    </Popover>
                    <Form.Item>
                        <Input  name="keywords" style={{width: 300}} placeholder={this.placeholder} defaultValue={pagination.keywords}/>
                    </Form.Item>
                    <Button type="primary" onClick={this.handleSearch} style={{"position":"relative","left":15,"top":3}}>搜索</Button>

                    <span style={{marginLeft: 30}}>
                                <Select defaultValue={pagination.vip_group} onChange={(v)=>this.vipGroupChange(v)} style={{"position":"relative","top":3,minWidth: 200}}>
                                    {
                                        this.vip_group_list.map(items =>
                                            <Option key={items.id} value={items.id}>{items.name}</Option>
                                        )
                                    }
                                </Select>
                            </span>
                </div>


            </Form>
            <div style={{position: 'relative',top: -15,left: 25}}>
                {
                    this.tagsRender()
                }
            </div>
        </div>
    }

    edit=async row=>{
        let inputRef=null;
        let that=this;
        await new Promise(resolve => {
            Modal.confirm({
                icon: <span></span>,
                title: '编辑',
                width:1000,
                content:
                    <Input.TextArea ref={r=>{
                        inputRef=r;
                    }
                    } defaultValue={row.cmd} rows={5} cols={10} />
                ,
                async onOk() {
                    let value=   inputRef.textAreaRef.value
                    let r= await MIO.configUpdate({id:row.id,useData:value});
                    if(M.checkR(r)){
                        that.fetch();
                    }
                    resolve();
                },
                onCancel() { },
            });
        });
    }


    //新增
    handleCreate(){

    }

    //获取数据
    async fetch() {
        this.setState({ loading: true });
        let { current,pageSize,keywords,order,filter } = this.state.pagination;
        let r=await window.M.request.get(this.fetchUrl,{
            page: current,
            num: pageSize,
            company: this.props.parentCurRecord.company,
            order: order,
            filter: JSON.stringify(filter)
        })
        let total =r.data.total;
        const pagination = { ...this.state.pagination };
        pagination.total = total;
        this.setState({
            pagination,
            data: r.data.dataList,
            loading: false
        })
    }




    actionRender(text, row, index) {
        let  CON_GROUP_SEL=this.CON_GROUP_SEL;

        let group_str_index=-1;
        if(CON_GROUP_SEL["2022展会抽奖1_威程"]==this.state.vip_group_id){
            group_str_index=0;
        }else if(CON_GROUP_SEL["2022展会抽奖2_代龙"]==this.state.vip_group_id){
            group_str_index=1;
        }else if(CON_GROUP_SEL["2022展会抽奖3_vip"]==this.state.vip_group_id){
            group_str_index=2;
        }else if(CON_GROUP_SEL["2022展会抽奖4_普通"]==this.state.vip_group_id){
            group_str_index=3;
        }


        return(
            <p >
                <Checkbox disabled={this.state.vip_group_id==-1} checked={row.group_str[group_str_index]=='1'} onChange={(v)=>{
                    if(this.state.vip_group_id==-1){
                        return;
                    }
                   let split= row.group_str.split("")

                    if(row.group_str[group_str_index]=='1'){

                        window.M.request.post("/statistics/member_config_vip_basic_group_rel",{
                            unionid:row.unionid,
                            vip_group_id:this.state.vip_group_id,
                            type:"delete"
                        })

                        split[group_str_index]=0+"";
                    }else {
                        window.M.request.post("/statistics/member_config_vip_basic_group_rel",{
                            unionid:row.unionid,
                            vip_group_id:this.state.vip_group_id,
                            type:"add"
                        });
                        split[group_str_index]=1+"";;
                    }
                    row.group_str=split.join("").trim();
                    console.log(row.group_str)
                    console.log(v.target.checked);
                    this.forceUpdate();
                }}></Checkbox>
            </p>
        )
    }

    getTotalNum(){

        let t0=0;
        let t1=0;
        let t2=0;
        let t3=0;
        for (let i=0;i<this.state.data.length;i++){
            let item=this.state.data[i];
            if(item.group_str[0]=='1'){
                t0++;
            }
            if(item.group_str[1]=='1'){
                t1++;
            }
            if(item.group_str[2]=='1'){
                t2++;
            }
            if(item.group_str[3]=='1'){
                t3++;
            }
        }
        return <div>
            共{this.state.data.length}人,选了[{t0},{t1},{t2},{t3}]人
        </div>
    }

    render() {
        return (
            <div>
                {this.inputRender()}
                {this.getTotalNum()}
                <Table
                    rowKey={record => record.id}
                    columns={this.columns}
                    dataSource={this.state.data}
                    pagination={this.state.pagination}
                    loading={this.state.loading}
                    scroll={{ x: this.tableWidth, y: 1000 }}
                    onRowClick={this.handleTableClick}
                    onChange={this.handleTableChange} />
            </div>
        )
    }


}











